//
//  QKMainTabBarController.m
//  QKWeibo-OC
//
//  Created by iMac_Kurt on 16/8/11.
//  Copyright © 2016年 Kurt_Qi. All rights reserved.
//

#import "QKMainTabBarController.h"
#import "QKHomeTableViewController.h"
#import "QKMessageTableViewController.h"
#import "QKDiscoverTableViewController.h"
#import "QKProfileTableViewController.h"

@interface QKMainTabBarController ()
@property (nonatomic, strong) UIButton *composeButtton;
@end

@implementation QKMainTabBarController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //
    [self addAllChildViewControllers];
    
    //
    [self addComposeButton];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.tabBar bringSubviewToFront:self.composeButtton];
}
#pragma mark - 添加所有子控制器
- (void)addAllChildViewControllers {
    // home
    QKHomeTableViewController *home = [[QKHomeTableViewController alloc] init];
    [self addOneChileViewController:home title:@"首页" imageName:@"tabbar_home"];
    // Message
    QKMessageTableViewController *message = [[QKMessageTableViewController alloc] init];
    [self addOneChileViewController:message title:@"消息" imageName:@"tabbar_message_center"];
    
    // ComposeButton: only for an area
    [self addOneChileViewController:[[UIViewController alloc]init] title:@"" imageName:@"tabbar_message_center"];
    
    // Discover
    QKDiscoverTableViewController *discover = [[QKDiscoverTableViewController alloc] init];
    [self addOneChileViewController:discover title:@"发现" imageName:@"tabbar_discover"];
    // Profile
    QKProfileTableViewController *profile = [[QKProfileTableViewController alloc] init];
    [self addOneChileViewController:profile title:@"我" imageName:@"tabbar_profile"];
}

- (void)addOneChileViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)name {
    
    vc.tabBarItem.image = [UIImage imageNamed:name];
    
    vc.title = title;
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
    
}

#pragma mark - 添加撰写按钮
- (void)addComposeButton {
    
    [self.tabBar addSubview:self.composeButtton];
    
    //
    NSUInteger count = self.childViewControllers.count;
    CGFloat w = self.tabBar.bounds.size.width / count;
    self.composeButtton.frame = CGRectInset(self.tabBar.bounds, w * 2 - 1, 0);
    
}

#pragma mark - ButtonClicked
- (void)composeButttonClicked:(UIButton *)sender {
    NSLog(@"ComposeButtonClicked");
}

#pragma mark - Lazy
- (UIButton *)composeButtton {
    if (_composeButtton == nil) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [button addTarget:self action:@selector(composeButttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [button sizeToFit];
        
        _composeButtton = button;
    }
    return _composeButtton;
}
@end
